<?php

class AdminUser extends CWebUser {
	
	const ROLE_OPERATOR = 'OperatorAccount';
	const ROLE_PROVIDER = 'ProviderAccount';
	const ROLE_DEALER = 'DealerAccount';
	const ROLE_MEMBER = 'MemberAccount';
	
	public $_roleConfig = null;
	protected $_worker = null;
	protected $_group = null;

	public function getGroupId() {
		return $this->getState('groupId') ? $this->getState('groupId') : '';
	}
	
	public function getGroup() {
		if(!$this->_group && !empty($this->groupId)) {
			$model = new GroupAccount();
			$this->_group = $model->findByPk($this->groupId);
		}
		return $this->_group;
	}
	
	public function getWorker() {
		if(!$this->_worker && !empty($this->role)) {
			$model = new $this->role;
			$this->_worker = $model->findByPk($this->id);
		}
		return $this->_worker;
	}
	
	public function getRoleConfig() {
		if(!$this->_roleConfig) {
			$config = Yii::app()->config->read('role.'.$this->role);
			$config['class'] = 'RoleConfig';
			$this->_roleConfig = Yii::createComponent($config);
		}
		return $this->_roleConfig;
	}
	
	public function getLoginUrl() {
		return AdminModule::DEFAULT_ROUTE.'login';
	}
	
	public function getRoleName() {
		return $this->roleConfig->roleName;
	}
	
	public function getMenu() {
		$config = Yii::app()->config->read($this->roleConfig->menu);
		return $config;
	}
	
	public function getReturnUrl() {
		return Yii::app()->createUrl($this->roleConfig->returnUrl);
	}
	
	public function getRole() {
		return $this->getState('role') ? $this->getState('role') : '';
	}
	
	protected function beforeLogin($id, $states, $fromCookie) {
		if($this->hasEventHandler('onBeforeLogin'))
		{
			$event=new CUserEvent($this, array(
				'id' => $id,
				'states' => $states,
				'fromCookie' => $fromCookie,
			));
			$this->onBeforeLogin($event);
			return $event->isValid;
		}
		else
			return true;
	}
	
	protected function afterLogin($fromCookie) {
		if($this->hasEventHandler('onAfterLogin')) {
			$event=new CEvent($this, array(
				'fromCookie' => $fromCookie,
			));
			$this->onAfterLogin($event);
		}
	}
	
	public function onBeforeLogin($event) {
		$this->raiseEvent('onBeforeLogin', $event);
	}
	
	public function onAfterLogin($event) {
		$this->raiseEvent('onAfterLogin', $event);
	}
	
	protected function beforeLogout() {
		if($this->hasEventHandler('onBeforeLogout'))
		{
			$event=new CUserEvent($this);
			$this->onBeforeLogout($event);
			return $event->isValid;
		}
		else
			return true;
	}
	
	protected function afterLogout() {
		if($this->hasEventHandler('onAfterLogout')) {
			$event=new CEvent($this);
			$this->onAfterLogout($event);
		}
	}
	
	public function onBeforeLogout($event) {
		$this->raiseEvent('onBeforeLogout', $event);
	}
	
	public function onAfterLogout($event) {
		$this->raiseEvent('onAfterLogout', $event);
	}
	
	
//	protected function afterLogin($fromCookie) {
//		$ucClient = Yii::app()->ucClient;
//		$user = $ucClient->user->userLogin($this->worker->worker_name, $this->worker->worker_password);
//		$sync = $ucClient->user->userSyncLogin($user->uid);
////		CVarDumper::dump($user->getErrorMsg());die;
//		$sync->register();
//	}
//	
//	protected function afterLogout() {
//		$ucClient = Yii::app()->ucClient;
//		$sync = $ucClient->user->userSyncLogout();
//		$sync->register();
//	}
	
}

class RoleConfig extends Config {
	public function items() {
		return array(
			'returnUrl' => '',
			'menu' => array(),
			'roleName' => '',
		);
	}
}

?>